#ifndef PARTICLE_EMIT
#define PARTICLE_EMIT
#include "Common.hlsl"
#include "ComputeBuffs.hlsl"

float rand(float3 co)
{
    return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 45.5432))) * 43758.5453);
}
float3 randomPointOnSphere(float u, float v, float radius)
{
    float theta = 2 * PI * u;
    float phi = acos(2 * v - 1);
    float x = radius * sin(phi) * cos(theta);
    float y = radius * sin(phi) * sin(theta);
    float z = radius * cos(phi);
    return float3(x, y, z);
}
AppendStructuredBuffer<uint> Particle_alive;//ready to add
ConsumeStructuredBuffer<uint> Particle_Pool;//ready to die
[numthreads(1, 1, 1)]
void Emit()
{
    
    uint id = Particle_Pool.Consume();
    Particle_alive.Append(id);
    Particle p = particles[id];

    p.alive = true;
    p.position = randomPointOnSphere(rand(seeds.xyz / (id + 1)),
        rand(seeds.yzx / (id + 1)), radius * rand(seeds.zyx / (id + 1)));
    
    float initialSpeed = initialSpeedRange.x + (initialSpeedRange.y - initialSpeedRange.x) * rand(seeds.xzy / (id + 1));
   
    p.velocity = normalize(p.position) * initialSpeed;
    p.velocity += inheritedVelocity;
    //p.position -= inheritedVelocity * rand(seeds.xzy / (id + 1)) * extrapolation;
    //p.position += inheritedPosition;
    
    p.life.x = 0.0f;
    p.life.y = lifeRange.x + (lifeRange.y - lifeRange.x) * rand(seeds.zxy / (id + 1));
 
    p.color = color;
    
    p.size = size;

    particles[id] = p;
}

#endif